/*
 * JDBC Url Parser for H2 database. Diluted version.
 *
 * License: LGPL v3
 */
package org.passwordstore.util;

/**
 *
 * @author Thotheolh
 */
public class H2JDBCParser {
    private String name;
    private String START_URL = "jdbc:h2:";
    private String dbName;
    private String baseDir;
    private boolean persistent = false;

    public H2JDBCParser(String url) {
        name = url;
        //System.out.println("Parsing: " + name);
        if (!url.startsWith(START_URL)) {
        } else {
            name = name.substring(START_URL.length());
            parseUrl();
        }
    }

    private void parseUrl() {
        parseName();
        setJDBCBaseDir(System.getProperty("user.home").toString());
        setDbBaseDir();
        System.out.println("Base Dir H2JDBCParser.parseUrl(): "+getBaseDir());
    }

    private void parseName() {
        if (".".equals(getName())) {
            setName("mem:");
        }
        if (getName().startsWith("tcp:")) {
            setName(getName().substring("tcp:".length()));
        } else if (getName().startsWith("ssl:")) {
            setName(getName().substring("ssl:".length()));
        } else if (getName().startsWith("mem:")) {
            setPersistent(false);
        } else if (getName().startsWith("file:")) {
            setName(getName().substring("file:".length()));
            setPersistent(true);
        } else {
            setPersistent(true);
        }
    }

    public void setJDBCBaseDir(String dir) {
        if (isPersistent()) {
            if (getName().startsWith("~")) {
                setName(removeCharAt(getName(), 0));
                setName(dir + getName());
                System.out.println("setJDBCBaseDir done..."+getName());
            }
        }
    }

    public static String removeCharAt(String s, int pos) {
        return s.substring(0, pos) + s.substring(pos + 1);
    }

    public void setDbBaseDir() {
        System.out.println("H2JDBCParser.setDbBaseDir() - Name: "+getName());
        String[] array = getName().split(";");
        String tmp = array[0].toString();
        array = null;
        array = tmp.split("/");
        StringBuffer buff = new StringBuffer();
        for(int i = 0; i<array.length;i++){
            System.out.println("H2JDBCParser.setDbBaseDir() - array["+i+"] : "+array[i]);
            if(i!=(array.length-1)){
                buff.append(array[i].toString()+"\\");
            }
        }
        setDbName(array[array.length - 1].toString());
        setBaseDir(buff.toString());
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the START_URL
     */
    public String getSTART_URL() {
        return START_URL;
    }

    /**
     * @param START_URL the START_URL to set
     */
    public void setSTART_URL(String START_URL) {
        this.START_URL = START_URL;
    }

    /**
     * @return the dbName
     */
    public String getDbName() {
        return dbName;
    }

    /**
     * @param dbName the dbName to set
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    /**
     * @return the baseDir
     */
    public String getBaseDir() {
        return baseDir;
    }

    /**
     * @return the persistent
     */
    public boolean isPersistent() {
        return persistent;
    }

    /**
     * @param persistent the persistent to set
     */
    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

    /**
     * @param baseDir the baseDir to set
     */
    public void setBaseDir(String baseDir) {
        this.baseDir = baseDir;
    }
}
